package cn.lg.soar.system.biz.modules.general.controller;

import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.model.UpdateDisabledVo;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.mvc.enums.LogModeEnum;
import cn.lg.soar.mvc.model.MvcProperties;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.LogConfigStatusEnum;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.general.entity.LogConfig;
import cn.lg.soar.system.biz.modules.general.service.LogConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Tag(name = "日志配置")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/log-config")
public class LogConfigController {

    @Autowired
    private LogConfigService service;
    @Autowired
    private MvcProperties properties;

    private final static String SUBJECT = "角色";

    @DisabledTenantAccess
    @Log(value = "新增" + SUBJECT, type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated LogConfig entity) {
        AssertUtil.isTrue(this.service.save(entity), "创建日志配置失败");
        return HttpResult.<Serializable>success().setData(entity.getId());
    }

    @CacheEvict(value = SystemCacheKey.LOG_CONFIG, key = "#p0.path", cacheManager = "remote")
    @DisabledTenantAccess
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated LogConfig entity) {
        AssertUtil.isTrue(this.service.updateById(entity), "更新日志配置失败");
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        AssertUtil.isTrue(this.service.removeByIds(ids), "删除日志配置失败");
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @UpdateDisabledMapping
    public HttpResult<Void> updateDisabled(@RequestBody @Validated UpdateDisabledVo vo) {
        AssertUtil.isTrue(
                this.service.update(
                        Wrappers.<LogConfig>lambdaUpdate()
                                .set(LogConfig::getStatus, vo.getDisabled() ? LogConfigStatusEnum.disabled : LogConfigStatusEnum.normal)
                                .in(LogConfig::getId, vo.getIds())
                ),
                vo.getDisabled() ? "禁用日志配置失败" : "启用日志配置失败"
        );
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<LogConfig> detail(@RequestParam(name = "id") @Id Long id) {
        LogConfig entity = this.service.getById(id);
        AssertUtil.notNull(entity, "日志配置不存在");
        return HttpResult.data(entity);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<LogConfig>> query(PageQuery pageQuery, QueryParams params){
        return HttpResult.data(
                new SqueryWapper<>(LogConfig.class).query(this.service, pageQuery, params)
        );
    }

    @DisabledTenantAccess
    @Operation(summary = "日志模式")
    @GetMapping("getMode")
    public HttpResult<LogModeEnum> getMode(){
        return HttpResult.data(properties.getLogMode());
    }

    @DisabledTenantAccess
    @Operation(summary = "刷新缓存")
    @GetMapping("refresh")
    public HttpResult<Void> refresh(){
        service.refresh();
        return HttpResult.success();
    }

}
